﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WeDonekRpc.Helper;

namespace WeDonekRpc.SqlSugar.jsonCovert
{
    internal class DictObjectConvert : JsonConverter<Dictionary<string, object>>
    {
        public override Dictionary<string, object>? ReadJson ( JsonReader reader, Type objectType, Dictionary<string, object>? existingValue, bool hasExistingValue, JsonSerializer serializer )
        {
            JObject obj = serializer.Deserialize<JObject>(reader);
            return obj.ToString().Json<Dictionary<string, object>>();
        }

        public override void WriteJson ( JsonWriter writer, Dictionary<string, object>? value, JsonSerializer serializer )
        {
            if ( value == null )
            {
                writer.WriteNull();
            }
            else
            {
                string json = value.ToJson();
                writer.WriteRawValue(json);
            }
        }
    }
}
